package com.songtech.uitest.module.rent;

import com.songtech.uitest.components.IBrowserDriverFactory;
import com.songtech.uitest.entities.Indent;
import com.songtech.uitest.module.common.*;
import com.songtech.uitest.module.propose.ProposeAudit;
import com.songtech.uitest.module.screen.ScreenAudit;
import com.songtech.uitest.util.Space;
import com.songtech.uitest.util.StepParser;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.ui.Select;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;


@Component
public class RentAudit {

    @Autowired
    @Qualifier("browserDriver")
    private IBrowserDriverFactory driverCreater;


    @Autowired
    @Qualifier("indent")
    private Indent indent;

    @Autowired
    private TabHandler tabHandler;

    @Autowired
    private MenuHandler menuHandler;

    @Autowired
    private AlertHandler alertHandler;

    @Autowired
    private KakongHandler kakongHandler;

    @Autowired
    private LogInAndOutHandler logInAndOutHandler;

    @Autowired
    private StepParser stepParser;

    private Logger logger = LoggerFactory.getLogger(RentAudit.class);

    //打印日志前的缩进空格
    @Autowired
    private Space space;


    /**
     * 起租阶段，1个单子进行起租审批。若是主账号则不含登录，若是其它账号则含其它账号登录
     * @param step 类似“02” 代表审批通过与否，0通过，1不通过；审批者。0为主账号，正整数为其他审核者（会导致新账号重新登录）
     * @throws Exception
     */
    public void rentOneAudit(String step) throws Exception{

        boolean approval = stepParser.parseApproval(step);
        WebDriver driver = stepParser.parseDriver(step);


        tabHandler.closeAllTabs(driver);

        menuHandler.clickMenuItem(driver, "铁塔建设", "起租确认审批", "");
        driver.switchTo().frame("iframeundefined");

        //查询此单
        Select logStatus = new Select(driver.findElement(By.id("logStatus")));
        logStatus.selectByVisibleText("待办");
        driver.findElement(By.id("keywords")).sendKeys(indent.getMobileId());
        driver.findElement(By.xpath("//button[text()='查询']")).click();
        Thread.sleep(6000);
        //查询并选择此条进行提交
        driver.findElement(By.xpath("//tbody/tr[1]/td[1]/input")).click();
        driver.findElement(By.xpath("//button[text()='审核']")).click();
        //转到起租确认审批详细页
        Thread.sleep(5000);
        //关闭上一个标签页
        tabHandler.closeFirstTab(driver);
        //回到起租确认审批详细页上
        WebElement frame = driver.findElements(By.name("iframe0")).get(1);
        driver.switchTo().frame(frame);

        //选择审核通过与否
        Select audit = new Select(driver.findElement(By.id("auditState")));
        audit.selectByIndex(approval ? 0 : 1); //0通过1不通过

        driver.findElement(By.id("auditNote")).sendKeys(approval ? "经审核同意提交的意见" : "经审核不同意提交的意见");

        driver.findElement(By.id("saveSet")).click();


        Thread.sleep(5000);

        //提交情况提示框出现,关掉它
        alertHandler.checkAndGo(driver);

        //记录单子的流程进行位置
        indent.setStepPosition(indent.getStepPosition() + 1);

        Thread.sleep(2000);

        String agree = approval ? "通过" : "不通过";
        logger.info(space.getSpace() + "起租当前审核步骤完成，审核意见为" + agree);

        //若是辅助账号及浏览器则退出登录
//        stepParser.teardown(step);


    }


    public void rent3OneAudit(String step) throws Exception{

        boolean approval = stepParser.parseApproval(step);
        WebDriver driver = stepParser.parseDriver(step);


        tabHandler.closeAllTabs(driver);

        menuHandler.clickMenuItem(driver, "三方建设", "起租确认审批", "");
        driver.switchTo().frame("iframeundefined");

        //查询此单
        Select logStatus = new Select(driver.findElement(By.id("logStatus")));
        logStatus.selectByVisibleText("待办");
        driver.findElement(By.id("keywords")).sendKeys(indent.getMobileId());
        driver.findElement(By.xpath("//button[text()='查询']")).click();
        Thread.sleep(6000);
        //查询并选择此条进行提交
        driver.findElement(By.xpath("//tbody/tr[1]/td[1]/input")).click();
        driver.findElement(By.xpath("//button[text()='审核']")).click();
        //转到三方起租确认审批详细页
        Thread.sleep(5000);
        //关闭上一个标签页
        tabHandler.closeFirstTab(driver);
        //回到三方起租确认审批详细页上
        WebElement frame = driver.findElements(By.name("iframe0")).get(1);
        driver.switchTo().frame(frame);

        //选择审核通过与否
        Select audit = new Select(driver.findElement(By.id("auditState")));
        audit.selectByIndex(approval ? 0 : 1); //0通过1不通过

        driver.findElement(By.id("auditNote")).sendKeys(approval ? "经审核同意提交的意见" : "经审核不同意提交的意见");

        driver.findElement(By.id("saveSet")).click();


        Thread.sleep(5000);

        //提交情况提示框出现,关掉它
        alertHandler.checkAndGo(driver);

        //记录单子的流程进行位置
        indent.setStepPosition(indent.getStepPosition() + 1);

        Thread.sleep(2000);

        String agree = approval ? "通过" : "不通过";
        logger.info(space.getSpace() + "三方起租当前审核步骤完成，审核意见为" + agree);


    }

}
